/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jerarquia;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alvaro
 */
public class grafica {
    
    public LinkedList<nodo_arbol> lista;
    public String path_dot="C:\\Program Files\\Graphviz 2.28\\bin\\dot.exe";
    public String fileOutputPath;
    
    
    public grafica(LinkedList<nodo_arbol> lst){
        lista=lst;
       fileOutputPath="";
    }
    
    
    public void graficar(String dir,String nombre,String hecho){
        FileWriter documento = null;
        String cadena="digraph G\n{\nnode [shape=record];\nnode [style=rounded];\n\n"+hecho+" [fillcolor=yellow, style=\"rounded,filled\", shape=record]\n";
        
        
        for(int i=0;i<lista.size();i++){
            
            cadena+=lista.get(i).nombre_nodo+lista.get(i).imprimir_hijos2()+"\n";
        }
        
        
        cadena+="\n}";
        
           try {
            documento = new FileWriter("C:\\Users\\Alvaro\\Desktop\\graficos\\"+nombre+".txt",true);
            PrintWriter puntero = new PrintWriter(documento);
            puntero.print(cadena);
            puntero.close();
            
            
             String fileInputPath = "C:\\Users\\Alvaro\\Desktop\\graficos\\"+nombre+".txt";
       fileOutputPath = "C:\\Users\\Alvaro\\Desktop\\graficos\\"+nombre+"_grafo.jpg";
       
      
      String tParam = "-Tjpg";
      String tOParam = "-o";
        
      String[] cmd = new String[5];
      cmd[0] = path_dot;
      cmd[1] = tParam;
      cmd[2] = fileInputPath;
      cmd[3] = tOParam;
      cmd[4] = fileOutputPath;
                  
      Runtime rt = Runtime.getRuntime();
      
      rt.exec( cmd );
            
            
            
        } catch (IOException ex) {
            Logger.getLogger(grafica.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                documento.close();
            } catch (IOException ex) {
                Logger.getLogger(grafica.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    
    
    
    
    
    
}
